package eteam.aps.systemservice.bll;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import eteam.aps.common.QueryResult;
import eteam.aps.common.enumeration.SortEnum;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.systeminterface.model.dto.EmpDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.EmpEntity;
import eteam.aps.systeminterface.model.enums.StateEnum;
import eteam.aps.systeminterface.model.reqparam.QueryEmpParam;
import eteam.aps.systemservice.dal.EmpDal;
import eteam.aps.systemservice.model.mapper.IEmpMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 职员相关业务
 */
@Service
public class EmpBll {
    @Autowired
    private EmpDal empDal;

    /**
     * 分页查询职员列表
     *
     * @param queryParam: 查询参数
     * @return: 分页数据
     */
    public QueryResult<EmpEntity> queryEmpPageList(QueryEmpParam queryParam) throws Exception {
        IPage<EmpEntity> page = new Page<EmpEntity>(queryParam.getPageIndex(), queryParam.getPageSize());
        QueryWrapper<EmpEntity> wrapper = new QueryWrapper<EmpEntity>();
        wrapper.eq("FOrgId", queryParam.getOrgid());
        if (queryParam.getState() > StateEnum.Delete.getValue()) {
            wrapper.eq("State", queryParam.getState());
        } else {
            wrapper.ne("State", StateEnum.Delete.getValue());
        }
        if (StringUtils.isNotBlank(queryParam.getKeyword())) {
            wrapper.and(i -> i.like("FCode", queryParam.getKeyword()).or().like("FName", queryParam.getKeyword()));
        }
        if (StringUtils.isNotBlank(queryParam.getSortFields()) && queryParam.getSortOrder().equals(SortEnum.Asc)) {
            wrapper.orderByAsc(queryParam.getSortFields());
        } else if (StringUtils.isNotBlank(queryParam.getSortFields()) && queryParam.getSortOrder().equals(SortEnum.Desc)) {
            wrapper.orderByDesc(queryParam.getSortFields());
        } else {
            wrapper.orderByAsc("FCode");
        }
        page = empDal.selectPageList(page, wrapper);
        QueryResult<EmpEntity> result = new QueryResult<EmpEntity>(Math.toIntExact(page.getTotal()), page.getRecords());
        return result;
    }

    /**
     * 创建职员
     *
     * @param empDto: 请求参数
     * @throws Exception
     */
    public void createEmp(VerifyTokenDto tokenDto, EmpDto empDto) throws Exception {
        EmpEntity empEntity = IEmpMapper.INSTANCE.toEntity(empDto);
        empEntity.setFempid(Helper.GetId());
        empEntity.setFrecorder(tokenDto.getFuserid());
        empEntity.setFrecordername(tokenDto.getFfullname());
        empEntity.setFrecordtime(DateUtil.date());
        int count = empDal.insertEmp(empEntity);
        if (count < 1) {
            throw new BusinessException("创建职员失败");
        }
    }

    /**
     * 更新职员
     *
     * @param empDto: 请求参数
     * @throws Exception
     */
    public void updateEmp(VerifyTokenDto tokenDto, EmpDto empDto) throws Exception {
        EmpEntity empEntity = IEmpMapper.INSTANCE.toEntity(empDto);
        empEntity.setFlastmodifier(tokenDto.getFuserid());
        empEntity.setFlastmodifiername(tokenDto.getFfullname());
        empEntity.setFlastmodifytime(DateUtil.date());
        int count = empDal.updateEmp(empEntity);
        if (count < 1) {
            throw new BusinessException("更新职员失败");
        }
    }

    /**
     * 删除职员
     *
     * @param empId: 职员ID
     * @throws Exception
     */
    public void deleteEmp(String empId) throws Exception {
        EmpEntity retObj = empDal.getById(empId);
        if (Objects.isNull(retObj)) {
            throw new BusinessException("职员不存在");
        }
        EmpEntity tmpObj = new EmpEntity();
        tmpObj.setFempid(empId);
        tmpObj.setFcode(StrUtil.format("{}_{}", retObj.getFcode(), System.currentTimeMillis()));
        tmpObj.setState(StateEnum.Delete.getValue());
        int count = empDal.updateEmp(tmpObj);
        if (count < 1) {
            throw new BusinessException("删除职员失败");
        }
    }
}
